package com.smallrig.mall.template.batch;


import com.smallrig.mall.template.entity.Order;
import com.smallrig.mall.template.service.OrderService;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//@Configuration
//@EnableBatchProcessing
public class BatchConfiguration {

    /**
     * 注入JobBuilderFactory
     */
    @Resource
    public JobBuilderFactory jobBuilderFactory;

    /**
     * 注入StepBuilderFactory
     */
    @Resource
    public StepBuilderFactory stepBuilderFactory;

    /**
     * 注入JobRepository
     */
    @Resource
    public JobRepository jobRepository;

    /**
     * 注入JobLauncher
     */
    @Resource
    private JobLauncher jobLauncher;

    @Resource
    private OrderService orderService;

    /**
     * 注入自定义job
     */
    @Resource
    private Job studentJob;

    /**
     * 封装writer bean
     *
     * @return
     */
//    @Bean
    public ItemWriter<Order> writer() {
        ItemWriter<Order> writer = new ItemWriter() {
            @Override
            public void write(List list) throws Exception {
                //debug发现是嵌套的List reader的线程List嵌套真正的List
                list.forEach((stu) -> {
                    for (Order order : (ArrayList<Order>) stu) {
                        orderService.save(order);
                    }
                });
            }
        };
        return writer;
    }

    /**
     * 封装reader bean
     *
     * @return
     */
//    @Bean
    public ItemReader<Order> reader() {
        ItemReader<Order> reader = new ItemReader() {
            @Override
            public Object read() throws Exception {

                return orderService.list();
            }
        };
        return reader;
    }

    /**
     * 封装processor bean
     *
     * @return
     */
//    @Bean
    public ItemProcessor processor() {
        ItemProcessor processor = new ItemProcessor() {
            @Override
            public Object process(Object o) throws Exception {
                //debug发现o就是reader单次单线程读取的数据
                return o;
            }
        };
        return processor;
    }

    /**
     * 封装自定义step
     *
     * @return
     */
//    @Bean
    public Step studentStepOne() {
        return stepBuilderFactory.get("studentStepOne")
            .chunk(1)
            .reader(reader()) //加入reader
            .processor(processor())  //加入processor
            .writer(writer())//加入writer
            .build();
    }

    /**
     * 封装自定义job
     *
     * @return
     */
    @Bean
    public Job studentJob() {
        return jobBuilderFactory.get("studentJob")
            .flow(studentStepOne())//加入step
            .end()
            .build();
    }


    /**
     * 使用spring 定时任务执行
     */
    @Scheduled(fixedRate = 5000)
    public void printMessage() {
        try {
            JobParameters jobParameters = new JobParametersBuilder()
                .addLong("time", System.currentTimeMillis())
                .toJobParameters();
            jobLauncher.run(studentJob, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
